import { asyncRoutes, constantRoutes } from '@/router'
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

function hasPermission1(type, route) {
  if (route.meta && route.meta.type && route.meta.roles) {
    return route.meta.type.indexOf(type) >= 0
  } else {
    return true
  }
}

/**
 * 生成扁平化机构路由(仅两级结构)
 */
function generateFlatRoutes(accessRoutes) {
  const flatRoutes = []

  for (const item of accessRoutes) {
    let childrenFflatRoutes = []
    if (item.children && item.children.length > 0) {
      childrenFflatRoutes = castToFlatRoute(item.children, '')
    }

    // 一级路由是布局路由,需要处理的只是其子路由数据
    flatRoutes.push({
      name: item.name,
      path: item.path,
      component: item.component,
      redirect: item.redirect,
      children: childrenFflatRoutes
    })
  }

  return flatRoutes
}
/**
 * 将子路由转换为扁平化路由数组（仅一级）
 * @param {待转换的子路由数组} routes
 * @param {父级路由路径} parentPath
 */
function castToFlatRoute(routes, parentPath, flatRoutes = []) {
  for (const item of routes) {
    if (item.children && item.children.length > 0) {
      if (item.redirect && item.redirect !== 'noRedirect') {
        flatRoutes.push({
          name: item.name,
          path: (parentPath + '/' + item.path).substring(1),
          redirect: item.redirect,
          meta: item.meta
        })
      }
      castToFlatRoute(item.children, parentPath + '/' + item.path, flatRoutes)
    } else {
      flatRoutes.push({
        name: item.name,
        path: (parentPath + '/' + item.path).substring(1),
        component: item.component,
        meta: item.meta
      })
    }
  }
  return flatRoutes
}

export function filterAsyncRoutes(routes, roles) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })
  return res
}

export function filterAsyncRoutes1(routes, type) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission1(type, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes1(tmp.children, type)
      }
      res.push(tmp)
    }
  })
  return res
}

const state = {
  routes: [],
  roles: [],
  sidebRoute: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_SIDEROUTE: (state, routes) => {
    state.sidebRoute = constantRoutes.concat(routes)
  }
}

const actions = {
  generateSidebarRoutes({ commit, state }, type) {
    const accessedRoutes1 = filterAsyncRoutes(asyncRoutes, state.roles)
    const accessedRoutes = filterAsyncRoutes1(accessedRoutes1, type)
    commit('SET_SIDEROUTE', accessedRoutes)
  },
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      commit('SET_ROLES', roles)
      const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // 重点修改  下边
      const flatRoutes = generateFlatRoutes(accessedRoutes)
      commit('SET_ROUTES', accessedRoutes)
      resolve(flatRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
